#include<iostream>

//请记住
/*
1、Copying函数应该确保复制“对象内的所有成员变量”及“所有base class 成分不要尝试以某个 copying函数实现另一个copying函数。
2、应该将共同机能放进第三个函数中，并由两个 coping函数共同调用
*/

class Customer
{
    public:
        Customer(){std::cout << "默认构造函数" << std::endl;};
        ~Customer(){std::cout << "析构函数" << std::endl;};
        Customer(const std::string &name)
        {
            std::cout << "构造函数" << std::endl;
            this->name_ = name;
        };
        Customer(const Customer &cust):name_(cust.name_)
        {
            std::cout << "复制构造函数" << std::endl;
        };

        Customer& operator=(const Customer &cust)
        {
            std::cout << "赋值操作符" << std::endl;
            if (this == &cust)
            {
                return *this;
            }

            this->name_ = cust.name_;
        };

        friend std::ostream& operator<<(std::ostream &cout,Customer &cust);
    private:
        std::string name_;
};

std::ostream& operator<<(std::ostream &cout,Customer &cust)
{
    if (&cust == nullptr)
    {
        return cout;
    }

    std::cout << "Customer name = " << cust.name_ << std::endl;
    return cout;  
}

class PrimaryCustomer:public Customer
{
    public:
        PrimaryCustomer(){std::cout << "PrimaryCustomer默认构造函数" << std::endl;data_ = 10;};
        ~PrimaryCustomer(){std::cout << "PrimaryCustomer析构函数" << std::endl;};
        PrimaryCustomer(const PrimaryCustomer &pcust):data_(pcust.data_),Customer(pcust)
        {std::cout << "PrimaryCustomer复制函数" << std::endl;}//基类也要进行赋值

        PrimaryCustomer& operator=(const PrimaryCustomer& pcust)
        {
            std::cout << "PrimaryCustomer赋值操作符" << std::endl;
            if (this == &pcust)
            {
                return *this;
            }

            Customer::operator=(pcust);//基类也要进行赋值
            this->data_ = pcust.data_;
            
            return *this;
        }
    private:
    int data_;
    
};

int main(int argc, char const *argv[])
{
    
    // Customer A("alang");
    // Customer B = A;
    // Customer C;
    // C = B;
    // std::cout << C;
    PrimaryCustomer pa;
    
    PrimaryCustomer pb = pa;

    PrimaryCustomer pc;
    pc = pb;
    return 0;
}
